﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Windows.Interop;
using System.Runtime.InteropServices;
using Microsoft.WindowsAPICodePack.Shell;

using Microsoft.Windows.Controls.Ribbon;
using ScoreMS.Views;

namespace ScoreMS
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            //GlassWindow.AeroGlassCompositionEnabled = false;

            this.MouseLeftButtonDown += new MouseButtonEventHandler(MainWindow_MouseLeftButtonDown);
            this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
        }

        #region 毛玻璃效果
        void OnLoaded(object sender, RoutedEventArgs e)
        {
            Transparent(this.Width, this.Height);
        }

        private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            Transparent(e.NewSize.Width, e.NewSize.Height);
        }

        void Transparent(double Width, double Height)
        {
            try
            {
                IntPtr mainWindowPtr = new WindowInteropHelper(this).Handle;
                HwndSource mainWindowSrc = HwndSource.FromHwnd(mainWindowPtr);
                mainWindowSrc.CompositionTarget.BackgroundColor = Color.FromArgb(0, 0, 0, 0);
                System.Drawing.Graphics desktop = System.Drawing.Graphics.FromHwnd(mainWindowPtr);
                float DesktopDpiX = desktop.DpiX;
                float DesktopDpiY = desktop.DpiY;
                NonClientRegionAPI.MARGINS margins = new NonClientRegionAPI.MARGINS();
                margins.cxLeftWidth = Convert.ToInt32(Width * (DesktopDpiX / 96));
                margins.cxRightWidth = Convert.ToInt32(Width * (DesktopDpiX / 96));
                margins.cyTopHeight = Convert.ToInt32(Height * (DesktopDpiX / 96));
                margins.cyBottomHeight = Convert.ToInt32(Height * (DesktopDpiX / 96));
                int hr = NonClientRegionAPI.DwmExtendFrameIntoClientArea(mainWindowSrc.Handle, ref margins);
            }
            catch (DllNotFoundException)
            {
                Application.Current.MainWindow.Background = Brushes.White;
            }
        }
        #endregion

        #region 窗体事件处理
        void MainWindow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }
        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            this.DataContext = App.MainViewModel;

            //测试！
            App.teacher_id = "CS100001";
            App.is_Logined = true;
        }
        #endregion

        #region 私有方法

        /// <summary>
        /// Frame导航到页面
        /// </summary>
        /// <param name="page"></param>
        private void GoTo(string page)
        {
            try
            {
                this.frame.Navigate(new Uri("Views/" + page + ".xaml", UriKind.Relative));
            }
            catch (Exception ex)        //try不出来？
            {
                MessageBox.Show("转到错误！" + ex.ToString());
            }
        }

        #endregion

        #region 按钮事件处理

        /// <summary>
        /// 导航事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RoutedEventHandler(object sender, RoutedEventArgs e)
        {
            Control btn = sender as Control;
            this.GoTo(btn.Tag.ToString());
        }

        /// <summary>
        /// 登陆
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            this.frame.Source = new Uri("Views/LoginPage.xaml", UriKind.Relative);
        }

        /// <summary>
        /// Frame的导航事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frame_Navigated(object sender, NavigationEventArgs e)
        {
            App.Current.Properties["frame"] = e.ExtraData;  
            //使用Application.Properties属性作为全局存储
        }
    }
        #endregion
    /// <summary>
    /// Win32 API 毛玻璃效果
    /// </summary>
    class NonClientRegionAPI
    {
        [StructLayout(LayoutKind.Sequential)]
        public struct MARGINS
        {
            public int cxLeftWidth;
            public int cxRightWidth;
            public int cyTopHeight;
            public int cyBottomHeight;
        };
        [DllImport("DwmApi.dll")]
        public static extern int DwmExtendFrameIntoClientArea(IntPtr hwnd, ref MARGINS pMarInset);
    }

}
